﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using libDAL;
using libInventory.BUS ;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
namespace libInventory.FORMS
{
    public partial class frmODChargesCustomizer : Telerik.WinControls.UI.RadForm
    {
        private string strChargeHeaderID = string.Empty;
        private string strChargeHeaderNewID = string.Empty;
        private clsInterfaceSalesTerm _BUS;
        private bool bolHeaderSaved = false;
        private bool bolEditMode = false;
     
        public frmODChargesCustomizer()
        {
            try
            {
                _BUS = new clsInterfaceSalesTerm();
                InitializeComponent();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " frmODChargesCustomizer"); 
            }
        }

        public bool get_EditStatus
        {
            get
            {
               return bolEditMode;
            }
            set
            {
                bolEditMode = value;
            }
        }

        private void set_LoadPortOrigin()
        {
            try
            {
                DataTable dt = new DataTable();

                dt = _BUS.get_Ports();
                mcmbOrigin.EditorControl.LoadElementTree();
                mcmbOrigin.DataSource = dt;

                for (int intIndex = 0; intIndex < dt.Columns.Count; intIndex++)
                {
                    mcmbOrigin.MultiColumnComboBoxElement.Columns[intIndex].BestFit();
                }

                mcmbOrigin.MultiColumnComboBoxElement.DropDownWidth = 300;
                mcmbOrigin.MultiColumnComboBoxElement.DropDownHeight = 150;
                mcmbOrigin.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LoadPortOrigin"); 
            }
        }

        private void set_LoadPortDestination()
        {
            try
            {
                DataTable dt = new DataTable();

                dt = _BUS.get_Ports();
                mcmbDestination.EditorControl.LoadElementTree();
                mcmbDestination.DataSource = dt;

                for (int intIndex = 0; intIndex < dt.Columns.Count; intIndex++)
                {
                    mcmbDestination.MultiColumnComboBoxElement.Columns[intIndex].BestFit();
                }

                mcmbDestination.MultiColumnComboBoxElement.DropDownWidth = 300;
                mcmbDestination.MultiColumnComboBoxElement.DropDownHeight = 150;
                mcmbDestination.SelectedIndex = -1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message +" set_LoadPortDestination"); 
            }
        }

        public string getset_ChargeHeaderID
        {
            get
            {
                return strChargeHeaderID;
            }
            set
            {
                strChargeHeaderID = value;
            }
        }

        private void set_Controls()
        {
            try
            {
                clsGenFunction.set_BackStage(ref rvCharges);
                ddlCategory.Size = new Size(119, 18);

                ((GridTableElement)this.dgvDestination.TableElement).RowHeight = 20;
                ((GridTableElement)this.dgvOrigin.TableElement).RowHeight = 20;
                ((GridTableElement)this.dgvAdditionalChargesDestination.TableElement).RowHeight = 20;
                ((GridTableElement)this.dgvAdditionalChargesOrigin.TableElement).RowHeight = 20;

                dteFrom.Value = clsGenFunction.get_DateDBServerFormat(_BUS.get_DAL);
                dteTo.Value = clsGenFunction.get_DateDBServerFormat(_BUS.get_DAL);
                dgvDestination.BestFitColumns();
                dgvOrigin.BestFitColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_Controls"); 
            }

        }

        public bool getset_HeaderSaved
        {
            get
            {
                return bolHeaderSaved;
            }
            set
            {
                bolHeaderSaved = value;
            }
        }

        private void set_DisableControlsHeaderDetails()
        {
            foreach (Control c in gbChargesHeader.Controls)
            {
                if (c.Name.ToLower().Contains("label") == false)
                {
                    c.Enabled = false;
                }
            }
        }

        private void set_EnableControlsHeaderDetails()
        {
            foreach (Control c in gbChargesHeader.Controls)
            {
                if (c.Name.ToLower().Contains("label") == false)
                {
                    c.Enabled = true;
                }
            }
        }

        private void set_EnableDetailsGrid()
        {
            dgvDestination.ReadOnly =false ;
            dgvOrigin.ReadOnly = false;
            dgvAdditionalChargesDestination.ReadOnly = false;
            dgvAdditionalChargesOrigin.ReadOnly = false; 
        }

        private void set_DissableDetailsGrid()
        {
            dgvDestination.ReadOnly = true ;
            dgvOrigin.ReadOnly = true;
            dgvAdditionalChargesDestination.ReadOnly = true;
            dgvAdditionalChargesOrigin.ReadOnly = true; 
        }

        private void frmOriginDestinationChargesCustomizer_Load(object sender, EventArgs e)
        {
            try
            {
                set_Controls();
                set_LoadPortDestination();
                set_LoadPortOrigin();

                set_DisableControlsHeaderDetails();
                set_DissableDetailsGrid(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " frmOriginDestinationChargesCustomizer_Load"); 
            }
        }

        private bool set_LoadGridOrigin()
        {
            bool bolStatus = false;
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " set_LoadGridOrigin");
            }
            return bolStatus;
        }

        private bool set_LoadGridOriginDestination(ref RadGridView dgvGrid, DataRow[] drSource)
        {
            bool bolStatus = false;
            try
            {
                if (dgvGrid.Rows.Count > 0)
                { dgvGrid.Rows.Clear(); }

                GridViewDataRowInfo rowInfo = new GridViewDataRowInfo(dgvGrid.MasterView);
                for (int intI = 0; intI < drSource.Length; intI++)
                {
                    rowInfo = new GridViewDataRowInfo(dgvGrid.MasterView);
                    for (int intC = 0; intC < dgvGrid.Columns.Count; intC++)
                    {
                        if (dgvGrid.Columns[intC].Name.ToString().Trim().ToLower().Contains("column") == false)
                        {
                            rowInfo.Cells[dgvGrid.Columns[intC].Name].Value = drSource[intI][dgvGrid.Columns[intC].Name];
                        }
                    }
                    dgvGrid.Rows.Add(rowInfo);
                }
                dgvGrid.BestFitColumns();
                bolStatus = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " set_LoadGridOriginDestination");
            }

            return bolStatus;
        }

        private bool set_UpdateOriginAdditionalDetails()
        {
            bool bolStatus = false;
            try
            {
                DataTable dtAdditionalOrigin = new DataTable();
                dtAdditionalOrigin = get_OriginDestinationDetails(ref dgvAdditionalChargesOrigin);
                if (set_TransferRecords(ref dtAdditionalOrigin, ref dgvAdditionalChargesOrigin) == true)
                {
                    bolStatus = _BUS.set_AddUpdateAdditionalChargesOriginDestination(dtAdditionalOrigin);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_UpdateOriginAdditionalDetails");
            }

            return bolStatus;
        }

        private bool set_UpdateDestinationAdditionalDetails()
        {
            bool bolStatus = false;
            try
            {
                DataTable dtAdditionalDestination = new DataTable();
                dtAdditionalDestination = get_OriginDestinationDetails(ref dgvAdditionalChargesDestination);
                if (set_TransferRecords(ref dtAdditionalDestination, ref dgvAdditionalChargesDestination) == true)
                {
                    bolStatus = _BUS.set_AddUpdateAdditionalChargesOriginDestination(dtAdditionalDestination);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_UpdateOriginAdditionalDetails");
            }

            return bolStatus;
        }

        private bool set_UpdateOriginDetails()
        {
            bool bolStatus = false;
            try
            {
                DataTable dtOrigin = new DataTable();
                dtOrigin = get_OriginDestinationDetails(ref dgvOrigin);
                if (set_TransferRecords(ref dtOrigin, ref dgvOrigin) == true)
                {
                    bolStatus = _BUS.set_AddUpdateOriginDestination(dtOrigin);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_UpdateOriginDetails"); 
            }

            return bolStatus;
       }

        private bool set_UpdateDestinationDetails()
        {
            bool bolStatus = false;
            try
            {
                DataTable dtDestination = new DataTable();
                dtDestination = get_OriginDestinationDetails(ref dgvDestination);
                if (set_TransferRecords(ref dtDestination, ref dgvDestination) == true)
                {
                    bolStatus = _BUS.set_AddUpdateOriginDestination(dtDestination);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_UpdateOriginDetails");
            }

            return bolStatus;
        }

        public bool set_SaveDetailsRecords()
        {
            bool bolStatus =false ;
            try
            {
                DataTable dtOrigin = new DataTable();
                DataTable dtDestination = new DataTable();
                bool bolstatusOrigin = false;
                bool bolstatusDestination = false;

                //updates the details fix charges origin and destination
                #region updates the details fix charges origin and destination
                    if (set_UpdateOriginDetails() == true 
                        && set_UpdateDestinationDetails() == true)
                    {
                        bolStatus = _BUS.set_UpdateRecordsDetails();
                    }
                #endregion

                #region updates the Additional Charges Origin and Destination
                    if (set_UpdateOriginAdditionalDetails() == true
                         && set_UpdateDestinationAdditionalDetails() == true)
                    {
                        bolStatus = _BUS.set_CommitRecordsAdditionalCharges(); 
                    }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_SaveRecords"); 
            }
            return bolStatus;
        }

        private void set_TemporaryDissableSelectedEvents()
        {
            this.dgvOrigin.CellValueChanged -= new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvOrigin_CellValueChanged);
            this.dgvDestination.CellValueChanged -= new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvDestination_CellValueChanged);
            this.dgvDestination.CurrentRowChanged -= new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvDestination_CurrentRowChanged);
            this.dgvOrigin.CurrentRowChanged -= new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvOrigin_CurrentRowChanged);
            this.dgvAdditionalChargesOrigin.CurrentRowChanged -= new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvAdditionalChargesOrigin_CurrentRowChanged);
            this.dgvAdditionalChargesDestination.CurrentRowChanged -= new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvAdditionalChargesDestination_CurrentRowChanged);
        }

        private void set_ActivateDissableSelectedEvents()
        {
            this.dgvOrigin.CurrentRowChanged += new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvOrigin_CurrentRowChanged);
            this.dgvOrigin.CellValueChanged += new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvOrigin_CellValueChanged);
            this.dgvDestination.CellValueChanged += new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvDestination_CellValueChanged);
            this.dgvDestination.CurrentRowChanged += new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvDestination_CurrentRowChanged);
            this.dgvAdditionalChargesOrigin.CurrentRowChanged += new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvAdditionalChargesOrigin_CurrentRowChanged);
            this.dgvAdditionalChargesDestination.CurrentRowChanged += new Telerik.WinControls.UI.CurrentRowChangedEventHandler(this.dgvAdditionalChargesDestination_CurrentRowChanged);
         
        }

        public bool set_NewRecords(string strSalesTermID)
        {
            bool bolStatus = false;
            try
            {
                bolStatus = get_LoadedChargesToGrids(strSalesTermID);
                if (bolStatus == true)
                {
                    if (_BUS.get_SetNewRowHeaderCharges() == true)
                    {
                        set_EnableControlsHeaderDetails();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_NewRecords"); 
            }
            return bolStatus;
        }

        private bool set_LoadOriginDestinationInDataGrids()
        {
            bool bolStatus = false;
            try
            {
                bool bolStatusOrigin = false;
                bool bolStatusDestination = false;
                bool bolAdditionalOrigin = false;
                bool bolAdditionalDestination = false;

                bolStatusOrigin = set_LoadGridOriginDestination(ref dgvOrigin, _BUS.get_OrginCharges);
                bolStatusDestination = set_LoadGridOriginDestination(ref dgvDestination, _BUS.get_DestinationCharges);
                bolAdditionalOrigin = set_LoadGridOriginDestination(ref dgvAdditionalChargesOrigin, _BUS.get_AdditionalChargesOrigin);
                bolAdditionalDestination = set_LoadGridOriginDestination(ref dgvAdditionalChargesDestination, _BUS.get_AdditionalChargesDestination);

                if (bolStatusOrigin == true && bolStatusDestination == true
                    && bolAdditionalDestination == true && bolAdditionalOrigin == true)
                {
                    bolStatus = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LoadOriginDestinationInDataGrids"); 
            }
            return bolStatus;
        }

        private bool get_LoadedChargesToGrids(string strSalesTermID)
        {
             bool bolStatus = false;
             try
             {
                 if (_BUS.set_RecordsOriginDestination(strSalesTermID) == true)
                 {
                     set_TemporaryDissableSelectedEvents();    
                     if (set_LoadOriginDestinationInDataGrids() == true)
                     {
                         set_ActivateDissableSelectedEvents();
                         bolStatus = true;
                     }
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.InnerException.Message + " get_IsLoadedCharges");
             }
             return bolStatus;
        }

        private void set_HeaderDetails()
        {
            try
            {
                txtDescription.Text = _BUS.getset_HeaderInfo[0]["Description"].ToString();
                ddlService.Text = _BUS.getset_HeaderInfo[0]["ServiceType"].ToString();
                mcmbOrigin.Text = _BUS.getset_HeaderInfo[0]["PortOrigin"].ToString();
                mcmbDestination.Text = _BUS.getset_HeaderInfo[0]["PortDestination"].ToString();

                dteFrom.Value = clsGenFunction.get_CheckDateTimeNull(_BUS.getset_HeaderInfo[0]["EffectiveDateFrom"]);
                dteTo.Value = clsGenFunction.get_CheckDateTimeNull(_BUS.getset_HeaderInfo[0]["EffectiveDateTo"]);

                ddlIncoterms.Text = _BUS.getset_HeaderInfo[0]["INCOTERM"].ToString();
                ddlType.Text = _BUS.getset_HeaderInfo[0]["Type"].ToString();
                ddlCategory.Text = _BUS.getset_HeaderInfo[0]["Category"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_HeaderDetails"); 
            }
        }

        private void set_LoadAdditionalCharges(string strSalesTermID)
        {
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LoadAdditionalCharges"); 
            }
        }

        public bool set_LoadExistingRecords(string strSalesTermID)
        {
            bool bolStatus = false;
            try
            {
              
                if(get_LoadedChargesToGrids(strSalesTermID) == true)
                {
                    if (_BUS.getset_HeaderInfo.Length > 0)
                    {
                        set_HeaderDetails();
                        getset_ChargeHeaderID = strSalesTermID;
                    }
                    get_EditStatus = true;
                    bolStatus = true;
                    getset_HeaderSaved = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " set_LoadRecords"); 
            }
            return bolStatus;
        }

        private void set_FindCharges(ref RadGridView dgvOriginDestination, bool bolOriginDestination)
        {
            try
            {
                using (frmFindCharges charges = new frmFindCharges())
                {
                    GridCellElement cellElement = dgvOriginDestination.TableElement.GetCellElement(dgvOriginDestination.CurrentRow, dgvOriginDestination.CurrentColumn);
                    Point cellLocation = dgvOriginDestination.PointToScreen(cellElement.ControlBoundingRectangle.Location);
                    charges.set_NewLocation(cellLocation.X, cellLocation.Y + dgvOriginDestination.CurrentCell.Size.Height);
                    charges.set_DAL = _BUS.get_DAL;

                    if (charges.ShowDialog() == DialogResult.OK)
                    {
                        if (_BUS.get_CheckDetailsIDExists(get_HeaderID().Trim() + charges.getset_Code.Trim(), bolOriginDestination) == false)
                        {
                            dgvOriginDestination.CurrentRow.Cells["ccitemcode"].Value = charges.getset_Code;
                            dgvOriginDestination.CurrentRow.Cells["columns"].Value = charges.getset_Description;
                            dgvOriginDestination.BestFitColumns();
                        }
                        else
                        {
                            MessageBox.Show("The selected Charge is duplicated, try again"); 
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_FindCharges");
            }
        }

        private void dgvDestination_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            try
            {
                if (e.Column.Index == 3 || e.Column.Index == 4)
                {
                    set_FindCharges(ref dgvDestination,false);
                }
                else
                {
                    _BUS.set_DestinationCharges(clsGenFunction.get_checkStringNull(dgvDestination.CurrentRow.Cells["SalesTermSetupDetailsID"].Value));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvDestination_CellClick"); 
            }
        }    

        private void radButton1_Click(object sender, EventArgs e)
        {
            dgvDestination.EndEdit();
        }

        private bool[] set_InitializeBoleanArray()
        {
            bool[] bolStatus = new bool[9];
            for (int intI = 0; intI < bolStatus.Length; intI++)
            {
                bolStatus[intI] = false;
            }

            return bolStatus;
        }

        private bool get_CheckFields(ref RadControl rc)
        {
            bool bolStatus = false;
            try
            {
                    RadTextBox txtBax = new RadTextBox();
                    RadDropDownList ddlList = new RadDropDownList();
                    RadMultiColumnComboBox mcmb = new RadMultiColumnComboBox();
                    Color clLocalColor;

                    #region ColorIdentification
                    if (rc.Text.Trim() == string.Empty)
                    {
                        clLocalColor = Color.Orange;
                    }
                    else
                    {
                        clLocalColor = Color.White;
                        bolStatus = true;
                    }
                    # endregion

                    if (rc.Name.Contains("Label") == false)
                    {
                        //checks if empty
                        txtBax = new RadTextBox();
                        ddlList = new RadDropDownList();
                        mcmb = new RadMultiColumnComboBox();
                        //change backcolor of textbox
                        if (rc is RadTextBox)
                        {
                            txtBax = (RadTextBox)rc;
                            txtBax.TextBoxElement.BackColor = clLocalColor;
                            rc = txtBax;
                        }
                        else if (rc is RadDropDownList)
                        {
                            //change backcolor of dropdownlist
                            ddlList = (RadDropDownList)rc;
                            ddlList.DropDownListElement.EditableElement.BackColor = clLocalColor;
                            rc = ddlList;
                        }
                        else if (rc is RadMultiColumnComboBox)
                        {
                            //change backcolor of dropdownlist
                            mcmb = (RadMultiColumnComboBox)rc;
                            mcmb.MultiColumnComboBoxElement.BackColor = clLocalColor;
                            rc = mcmb;
                        }
                    }
                    else
                    {
                        bolStatus = true;
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " frmAddressBook_get_CheckFields");
            }
            return bolStatus;
        }

        private bool get_ValidateHeader()
        {
            
            bool bolStatus = false;
            try
            {
                RadControl rc = new RadControl();
                int intCounter = 0;
                foreach (RadControl ctrl in this.Controls)
                {
                    // You can use the following if condition to target the specific control
                    if (ctrl.Name.Equals("gbChargesHeader"))
                    {
                        foreach (Control c in gbChargesHeader.Controls)
                        {
                            rc = c as RadControl;
                            // This foreach loop will enable all the controls within groupbox
                            if (rc.GetType() == typeof(RadTextBox) || rc.GetType() == typeof(RadDropDownList) ||
                                rc.GetType() == typeof(RadMultiColumnComboBox))
                            {
                                if (get_CheckFields(ref rc) == false)
                                {
                                    intCounter++;
                                }
                            }
                           
                        }
                    }
                }
                if (intCounter == 0)
                {
                    bolStatus = true;
                }
            }
            catch (Exception ex )
            {
                MessageBox.Show(ex.InnerException.Message + " get_ValidateHeader"); 
            }


            return bolStatus;
        }

        private void dgvDestination_RowValidating(object sender, RowValidatingEventArgs e)
        {
            if (_BUS.get_checkNewRowSetup() == true)
            {
                _BUS.set_AddDataRowOriginDestination();
            }
        }

        private void set_CheckGridFieldEntry(CellValidatingEventArgs e)
        {
            try
            {
                if (e.ColumnIndex >= 4)
                {
                    if (e.ColumnIndex < 17)
                    {
                        if (e.Value != null)
                        {

                        }
                        else if (Convert.ToString(e.Value) == string.Empty || e.Value == null)
                        {
                            MessageBox.Show("requred fields or set the value to zero (0) ");
                            e.Cancel = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_CheckGridFieldEntry"); 
            }
        }

        private void dgvDestination_CellValidating(object sender, CellValidatingEventArgs e)
        {
            set_CheckGridFieldEntry(e);
        }

        private void dgvOrigin_CellValidating(object sender, CellValidatingEventArgs e)
        {
          //  set_CheckGridFieldEntry(e);
        }

        //reminder put a flag that will enable the grid for entry i.e. no header ID 
        //then the grid must not be available for entry.

        private string get_HeaderID()
        {
            string strID = string.Empty;
            try
            {
                //if (get_ValidateHeader() == true)
                //{
                    strID = ddlIncoterms.Text.Trim()  +"-"+ ddlType.Text.Trim().Substring(0, 1) +
                            ddlCategory.Text.Trim().Substring(0, 1) + mcmbOrigin.Text.Trim().Substring(0, 1) + mcmbDestination.Text.Trim().Substring(0, 1) +
                            dteFrom.Value.ToString("ddMMyy").Substring(0, 2) + dteTo.Value.ToString("ddMMyy").Substring(2, 2);

                    strID = strID + "*" +ddlService.Text.Trim () +"_"+ txtDescription.Text.Trim() + "_" + ddlType.Text.Trim() +
                             ddlCategory.Text.Trim() + "_" + mcmbOrigin.Text.Trim() + "_" + mcmbDestination.Text.Trim() +
                             dteFrom.Value.ToString("dd-MM-yyyy") + "_" + dteTo.Value.ToString("dd-MM-yyyy");
                }
           // }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " get_HeaderID"); 
            }
            return strID;
        }

        private void set_dgvOriginCellValueChanged()
        {
            try
            {
                if (this.IsLoaded == true && _BUS.get_OriginDestinationLoaded == true)
                {
                    if (_BUS.get_checkNewRowSetup() == false)
                    {
                       // _BUS.get_SetNewRowDetailsCharges();
                    }

                    for (int intCol = 0; intCol < dgvOrigin.Columns.Count; intCol++)
                    {
                        if (dgvOrigin.Columns[intCol].Name.ToString().ToLower().Contains("column") == false)
                        {
                            if (dgvOrigin.Columns[intCol].Name.ToString().ToLower().Contains("ccitemcode") == true)
                            {
                                _BUS.getset_NewRowDetailsCharge[dgvOrigin.CurrentRow.Cells[intCol].ColumnInfo.Name] = clsGenFunction.get_checkStringNull(dgvOrigin.CurrentRow.Cells[intCol].Value);
                            }
                            else if (dgvOrigin.Columns[intCol].Name.ToString().ToLower().Contains("tasexpiry") == false)
                            {
                                _BUS.getset_NewRowDetailsCharge[dgvOrigin.CurrentRow.Cells[intCol].ColumnInfo.Name] = clsGenFunction.get_checkComputeNullDbl(dgvOrigin.CurrentRow.Cells[intCol].Value);
                            }
                            else if (dgvOrigin.Columns[intCol].Name.ToString().ToLower().Contains("tasexpiry") == true)
                            {
                                if (dgvOrigin.CurrentRow.Cells[intCol].Value == null)
                                {
                                    _BUS.getset_NewRowDetailsCharge[dgvOrigin.CurrentRow.Cells[intCol].ColumnInfo.Name] = DBNull.Value;
                                }
                            }
                        }
                    }
                    _BUS.getset_NewRowDetailsCharge["ChargesAmountHeaderID"] = get_HeaderID();
                    _BUS.getset_NewRowDetailsCharge["ChargesAmountDetailsID"] = clsGenFunction.get_checkStringNull(dgvOrigin.CurrentRow.Cells["ccitemcode"].Value) + "_" + get_HeaderID();
                    _BUS.getset_NewRowDetailsCharge["ChargeType"] = "Origin";
                    //ChargeType
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_dgvOriginCellValueChanged");
            }
        }

        private void dgvOrigin_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            try
            {
                //if (get_EditStatus == false)
                //{
                 //   set_dgvOriginCellValueChanged();
                //}
                //else if(get_EditStatus == true )
                //{
                //    set_dgvOriginDestinationCellValueChangedEdit(e, ref dgvOrigin);
                //}
            }
            catch (Exception ex)
            { 
                MessageBox.Show(ex.InnerException.Message + " dgvOrigin_CellValueChanged"); 
            }
        }

        private void set_ReadOnlyColumnsOrigin(int intColumnIndex)
        {
            for (int intCol = intColumnIndex; intCol < dgvOrigin.Columns.Count; intCol++)
            {
                dgvOrigin.Columns[intCol].ReadOnly = true;
            }
            
        }

        //private void set_dgvOriginCellClick()
        //{
        //    try
        //    {
        //        if (dgvDestination.ReadOnly == false && dgvOrigin.ReadOnly == false)
        //        {
        //            using (frmFindCharges charges = new frmFindCharges())
        //            {
        //                GridCellElement cellElement = this.dgvOrigin.TableElement.GetCellElement(this.dgvOrigin.CurrentRow,
        //                                                                                         this.dgvOrigin.CurrentColumn);
        //                Point cellLocation = this.dgvOrigin.PointToScreen(cellElement.ControlBoundingRectangle.Location);
        //                charges.set_NewLocation(cellLocation.X, cellLocation.Y + dgvOrigin.CurrentCell.Size.Height);
        //                charges.set_DAL = _BUS.get_DAL;

        //                if (charges.ShowDialog() == DialogResult.OK)
        //                {
        //                     dgvOrigin.CurrentRow.Cells[2].Value = charges.getset_Code;
        //                     dgvOrigin.CurrentRow.Cells[3].Value = charges.getset_Description;
        //                     dgvOrigin.BestFitColumns();
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message + " set_dgvDestinationCellValueChanged");  
        //    }
        //}

        private void dgvOrigin_CellClick(object sender, GridViewCellEventArgs e)
        {
            try
            {
             //   this.dgvOrigin.CellClick -= new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvOrigin_CellClick);
                if (e.Column.Index == 3 || e.Column.Index == 4)
                {
                    set_FindCharges(ref dgvOrigin, true);
                }
                else
                {
                    _BUS.set_OriginCharges(clsGenFunction.get_checkStringNull(dgvOrigin.CurrentRow.Cells["SalesTermSetupDetailsID"].Value));
                }
            //    this.dgvOrigin.CellClick += new Telerik.WinControls.UI.GridViewCellEventHandler(this.dgvOrigin_CellClick);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvOrigin_CellClick"); 
            }
        }

        //private void set_DestinationCellValueChanged()
        //{
        //    try
        //    {
        //        if (this.IsLoaded == true && _BUS.get_OriginDestinationLoaded == true)
        //        {
        //            if (_BUS.get_checkNewRowSetup() == false)
        //            {
        //                _BUS.get_SetNewRowDetailsCharges();
        //            }

        //            for (int intCol = 0; intCol < dgvDestination.Columns.Count; intCol++)
        //            {
                      //  if (dgvDestination.Columns[intCol].Name.ToString().ToLower().Contains("column") == false)
                        //{
                        //    if (dgvDestination.Columns[intCol].Name.ToString().ToLower().Contains("ccitemcode") == true)
                        //    {
                        //        _BUS.getset_NewRowDetailsCharge[dgvDestination.CurrentRow.Cells[intCol].ColumnInfo.Name] = clsGenFunction.get_checkStringNull(dgvDestination.CurrentRow.Cells[intCol].Value);
                        //    }
                        //    else if (dgvDestination.Columns[intCol].Name.ToString ().ToLower().Contains ("tasexpiry") == false)
                        //    {
                        //        _BUS.getset_NewRowDetailsCharge[dgvDestination.CurrentRow.Cells[intCol].ColumnInfo.Name] = clsGenFunction.get_checkComputeNullDbl(dgvDestination.CurrentRow.Cells[intCol].Value);
                        //    }
                        //    else if (dgvDestination.Columns[intCol].Name.ToString().ToLower().Contains("tasexpiry") == true)
                        //    {
                        //        if (dgvDestination.CurrentRow.Cells[intCol].Value == null)
                        //        {
                        //            _BUS.getset_NewRowDetailsCharge[dgvDestination.CurrentRow.Cells[intCol].ColumnInfo.Name] = DBNull.Value;
                        //        }
                        //    }
                        //}
                   // }
        //            _BUS.getset_NewRowDetailsCharge["ChargesAmountHeaderID"] = get_HeaderID();
        //            _BUS.getset_NewRowDetailsCharge["ChargesAmountDetailsID"] = get_HeaderID() + "_" + clsGenFunction.get_checkStringNull(dgvDestination.CurrentRow.Cells["ccitemcode"].Value);
        //            _BUS.getset_NewRowDetailsCharge["ChargeType"] = "Destination";
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message + " set_DestinationCellValueChanged");
 
        //    }
        //}

        private void dgvDestination_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            try
            {
                //if (get_EditStatus == false)
                //{
                  //  set_DestinationCellValueChanged();
                //}
                //else if (get_EditStatus == true)
                //{
             //  set_dgvOriginDestinationCellValueChangedEdit(e, ref dgvDestination);
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message + " dgvOrigin_CellValueChanged");
            }
        }

        private string get_SalesTermID()
        {
            return ddlService.Text.Trim() + "-" + ddlIncoterms.Text.Trim() + "-" + ddlType.Text.Trim() + "-" +
                   mcmbOrigin.Text.Trim() + "-" + mcmbDestination.Text.Trim() + "-" + ddlCategory.Text.Trim(); 
        }

        private void dgvOrigin_RowValidating(object sender, RowValidatingEventArgs e)
        {

          //  MessageBox.Show("dgvOrigin_RowValidating Total No. of Rows: " + dgvOrigin.Rows.Count.ToString() + " new row index = " + e.Row.Index.ToString());

            //if (_BUS.get_checkNewRowSetup() == true)
            //{
            //    _BUS.set_AddDataRowOriginDestination();
            //}
        }

        public void set_Edit()
        {
            try
            {
                getset_HeaderSaved = false;
                get_EditStatus = true;
                set_EnableDetailsGrid();
                set_EnableControlsHeaderDetails();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_Edit"); 
            }
        }

        private bool set_HeaderUpdateInformation()
        {
            bool bolStatus = false;
            try
            {
                if (_BUS.getset_HeaderInfo.Length > 0)
                {
                    _BUS.getset_HeaderInfo[0]["SalesTermSetupHeadersID"] = getset_ChargeHeaderID;
                    _BUS.getset_HeaderInfo[0]["Description"] = txtDescription.Text.Trim();
                    _BUS.getset_HeaderInfo[0]["INCOTERM"] = ddlIncoterms.Text.Trim();
                    _BUS.getset_HeaderInfo[0]["Type"] = ddlType.Text.Trim();
                    _BUS.getset_HeaderInfo[0]["Category"] = ddlCategory.Text.Trim();
                    _BUS.getset_HeaderInfo[0]["PortOrigin"] = mcmbOrigin.Text.Trim();
                    _BUS.getset_HeaderInfo[0]["PortDestination"] = mcmbDestination.Text.Trim();
                    _BUS.getset_HeaderInfo[0]["EffectiveDateFrom"] = dteFrom.Value;
                    _BUS.getset_HeaderInfo[0]["EffectiveDateTo"] = dteTo.Value;
                    _BUS.getset_HeaderInfo[0]["ServiceType"] = ddlService.Text.Trim();
                    _BUS.getset_HeaderInfo[0]["SalesTermID"] = get_SalesTermID();
                }
                else
                {
                    MessageBox.Show("Sales Term Header ID is not found");
                }
                bolStatus = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_HeaderUpdateInformation");
            }
            return bolStatus;
        }

        private bool set_HeaderInformation()
        {
            bool bolStatus = false;
            try
            {
                getset_ChargeHeaderID = get_HeaderID();
                _BUS.getset_NewRowHeaderCharges["SalesTermSetupHeadersID"] = getset_ChargeHeaderID;
                _BUS.getset_NewRowHeaderCharges["Description"] = txtDescription.Text.Trim();
                _BUS.getset_NewRowHeaderCharges["INCOTERM"] = ddlIncoterms.Text.Trim();
                _BUS.getset_NewRowHeaderCharges["Type"] = ddlType.Text.Trim();
                _BUS.getset_NewRowHeaderCharges["Category"] = ddlCategory.Text.Trim();
                _BUS.getset_NewRowHeaderCharges["PortOrigin"] = mcmbOrigin.Text.Trim();
                _BUS.getset_NewRowHeaderCharges["PortDestination"] = mcmbDestination.Text.Trim();
                _BUS.getset_NewRowHeaderCharges["EffectiveDateFrom"] = dteFrom.Value;
                _BUS.getset_NewRowHeaderCharges["EffectiveDateTo"] = dteTo.Value;
                _BUS.getset_NewRowHeaderCharges["ServiceType"] = ddlService.Text.Trim();
                _BUS.getset_NewRowHeaderCharges["SalesTermID"] = get_SalesTermID();
                bolStatus = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "");
            }
            return bolStatus;
        }

        public bool set_SaveHeaderRecords()
        {
            bool bolstatus = false;
            try
            {
                if (txtID.Text.Trim() == string.Empty || get_EditStatus == true)
                {
                    //there must be a getset to hold the current header ID
                    //this is to be utilized to identify the mode it's either edit or new
                    if (get_HeaderID() == getset_ChargeHeaderID)
                    {
                        if (_BUS.get_CheckHeaderExists(get_HeaderID()) == true)
                        {
                            if (set_HeaderUpdateInformation() == true)
                            {
                                if (_BUS.set_CommitRecordsHeader() == true)
                                {
                                    set_DisableControlsHeaderDetails();
                                    set_EnableDetailsGrid();
                                    getset_HeaderSaved = true;
                                    txtID.Text = getset_ChargeHeaderID.Substring(0, getset_ChargeHeaderID.IndexOf("*"));
                                    bolstatus = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (_BUS.get_CheckHeaderExists(get_HeaderID()) == false)
                        {
                            if (_BUS.get_SetNewRowHeaderCharges() == true)
                            {
                                if (set_HeaderInformation() == true)
                                {
                                    if (_BUS.set_AddDataHeader() == true)
                                    {
                                        if (_BUS.set_CommitRecordsHeader() == true)
                                        {
                                            set_DisableControlsHeaderDetails();
                                            set_EnableDetailsGrid();
                                            getset_HeaderSaved = true;
                                            txtID.Text = getset_ChargeHeaderID.Substring(0, getset_ChargeHeaderID.IndexOf("*"));
                                            getset_ChargeHeaderID = get_HeaderID();
                                            bolstatus = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                bolstatus = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_SaveHeaderRecords");
            }
            return bolstatus;
        }

        private void set_CurrentRowchanged(CurrentRowChangedEventArgs e, ref RadGridView dgvOriginDestination, string strType)
        {
            try
            {
                bool bolOriginDestination = false;

                if (strType.Trim() == "Origin")
                {bolOriginDestination = true;}
                
                if (e.CurrentRow.Index > -1)
                {
                    if (_BUS.get_CheckDetailsIDExists(get_HeaderID().Trim() + dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ccitemcode"].Value, bolOriginDestination) == false)
                    {
                        if (set_CheckExists(get_HeaderID().Trim() + dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ccitemcode"].Value, ref dgvOriginDestination) == false)
                        {
                            dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["SalesTermSetupDetailsID"].Value = get_HeaderID().Trim() +
                            dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ccitemcode"].Value;

                              dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["SalesTermSetupHeadersID"].Value = get_HeaderID().Trim();
                            
                            if (strType.Trim() == "Origin")
                            {
                                dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ChargeType"].Value = "Origin";
                            }
                            else if (strType.Trim() == "Destination")
                            {
                                dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ChargeType"].Value = "Destination";
                            }
                        }
                        else
                        {
                            //message box here saying that the sales term is existing or duplicated
                            //dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Delete();
                        }
                    }
                    else
                    {
                        //message box here saying that the sales term is existing or duplicated
                        //dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvOrigin_CurrentRowChanged");
            }
        }

        private void set_CurrentRowchangedAdditionalCharges(CurrentRowChangedEventArgs e, ref RadGridView dgvOriginDestination, string strType)
        {
            try
            {
                bool bolOriginDestination = false;
                if (strType.Trim() == "Origin")
                { bolOriginDestination = true; }

                if (e.CurrentRow.Index > -1)
                {
                    if (_BUS.get_CheckAdditionalChargesIDExists(get_HeaderID().Trim() + dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ccitemcode"].Value, bolOriginDestination) == false)
                    {
                        if (set_CheckExistsAdditionalCharges(get_HeaderID().Trim() + dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ccitemcode"].Value, ref dgvOriginDestination) == false)
                        {
                            dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["SalesTermSetupAdditionalChargesID"].Value = get_HeaderID().Trim() +
                            dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ccitemcode"].Value;

                            dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["SalesTermSetupHeadersID"].Value = get_HeaderID().Trim();

                            if (strType.Trim() == "Origin")
                            {
                                dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ChargeType"].Value = "Origin";
                            }
                            else if (strType.Trim() == "Destination")
                            {
                                dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Cells["ChargeType"].Value = "Destination";
                            }
                        }
                        else
                        {
                            //message box here saying that the sales term is existing or duplicated
                            //dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Delete();
                        }
                    }
                    else
                    {
                        //message box here saying that the sales term is existing or duplicated
                        //dgvOriginDestination.Rows[dgvOriginDestination.Rows.Count - 1].Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvOrigin_CurrentRowChanged");
            }
        }


        private void dgvOrigin_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            try
            {
                if (e.CurrentRow.Index > -1)
                {
                    set_CurrentRowchanged(e, ref dgvOrigin,"Origin");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvOrigin_CurrentRowChanged"); 
            }
        }

        private bool set_CheckExistsAdditionalCharges(string strID, ref RadGridView dgvOriginDestination)
        {
            bool bolStatus = false;
            try
            {
                for (int intRows = 0; intRows < dgvOriginDestination.Rows.Count; intRows++)
                {
                    if (dgvOriginDestination.Rows[intRows].Cells["SalesTermSetupAdditionalChargesID"].Value != null)
                    {
                        if (dgvOriginDestination.Rows[intRows].Cells["SalesTermSetupAdditionalChargesID"].Value.ToString().Trim() == strID.Trim())
                        {
                            bolStatus = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_CheckExists");
            }

            return bolStatus;
        }

        private bool set_CheckExists(string strID, ref RadGridView dgvOriginDestination)
        {
            bool bolStatus = false;
            try
            {
                for (int intRows = 0; intRows < dgvOriginDestination.Rows.Count; intRows++)
                {
                    if (dgvOriginDestination.Rows[intRows].Cells["SalesTermSetupDetailsID"].Value != null)
                    {
                        if (dgvOriginDestination.Rows[intRows].Cells["SalesTermSetupDetailsID"].Value.ToString().Trim() == strID.Trim())
                        {
                            bolStatus = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_CheckExists"); 
            }

            return bolStatus;
        }

        private void dgvDestination_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
        {
            //if (_BUS.get_checkNewRowSetup() == false)
            //{
             //   _BUS.get_SetNewRowDetailsCharges();
            //}
        }

        private DataTable get_OriginDestinationDetails(ref RadGridView dgvColumnSource)
        {
            DataTable dt = new DataTable();
            try
            {
                if (dgvColumnSource.Columns.Count > 0)
                {
                    for (int intCol = 0; intCol < dgvColumnSource.Columns.Count; intCol++)
                    {
                        if (dgvColumnSource.Columns[intCol].Name.ToString().Trim().Contains("column") == false)
                        {
                            dt.Columns.Add(dgvColumnSource.Columns[intCol].Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_OriginDestination"); 
            }
            return dt;
        }

        private bool set_TransferRecords(ref DataTable dtStorage, ref RadGridView dgvSource)
        {
            bool bolStatus = false;
            try
            {
                DataRow drInsert;
                for (int intRow = 0; intRow < dgvSource.Rows.Count; intRow++)
                {
                    drInsert = dtStorage.NewRow();
                    for (int intCol = 0; intCol < dtStorage.Columns.Count; intCol++)
                    {
                        if (dtStorage.Columns[intCol].ColumnName.ToLower().Contains("column") == false)
                        {
                            if (dgvSource.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value != null && 
                                dgvSource.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value != DBNull.Value)
                            {
                                if (dgvSource.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value.GetType() == typeof(string))
                                {
                                    //SalesTermSetupHeadersID
                                    if (dtStorage.Columns[intCol].ColumnName.Trim() == "SalesTermSetupHeadersID")
                                    {
                                        dgvSource.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value = get_HeaderID(); 
                                    }
                                    drInsert[dtStorage.Columns[intCol].ColumnName] = clsGenFunction.get_checkStringNull(dgvSource.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value);
                                }
                                else if (dgvSource.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value.GetType() == typeof(DateTime))
                                {
                                    drInsert[dtStorage.Columns[intCol].ColumnName] = dgvSource.Rows[intRow].Cells[dtStorage.Columns[intCol].ColumnName].Value;
                                }
                            }
                        }
                    }
                    dtStorage.Rows.Add(drInsert);
                }
                bolStatus = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_TransferRecords"); 
            }
            return bolStatus;
        }

        private void radButton1_Click_1(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                for (int intCol = 0; intCol < dgvOrigin.Columns.Count; intCol++)
                {
                    if (dgvOrigin.Columns[intCol].Name.ToString().ToLower().Contains("column") == false)
                    {
                          dt.Columns.Add(dgvOrigin.Columns[intCol].Name.ToString());
                    }
                }


                DataRow drx = dt.NewRow();
                string a = string.Empty;
                for (int intRow = 0; intRow < dgvOrigin.Rows.Count; intRow++)
                {
                    drx = dt.NewRow();
                    for (int intCol = 0; intCol < dt.Columns.Count; intCol++)
                    {
                        drx[intCol] = clsGenFunction.get_checkStringNull(dgvOrigin.Rows[intRow].Cells[intCol].Value);
                    }
                    dt.Rows.Add(drx);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " radButton1_Click_1"); 
            }
        }
        
        private void dgvDestination_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            try
            {
                if (e.CurrentRow.Index > -1)
                {
                    set_CurrentRowchanged(e, ref dgvDestination,"Destination");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvDestination_CurrentRowChanged"); 
            }
        }

        private void dgvAdditionalChargesOrigin_CellClick(object sender, GridViewCellEventArgs e)
        {
            try
            {
                 
                if (e.Column.Index == 3 || e.Column.Index == 4)
                {
                    set_FindCharges(ref dgvAdditionalChargesOrigin, false);
                }
                else
                {
                    _BUS.set_DestinationCharges(clsGenFunction.get_checkStringNull(dgvAdditionalChargesOrigin.CurrentRow.Cells["SalesTermSetupAdditionalChargesID"].Value));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvAdditionalChargesOrigin_CellClick");
            }
        }

        private void dgvAdditionalChargesDestination_CellClick(object sender, GridViewCellEventArgs e)
        {
            try
            {
                if (e.Column.Index == 3 || e.Column.Index == 4)
                {
                    set_FindCharges(ref dgvAdditionalChargesDestination, false);
                }
                else
                {
                    _BUS.set_DestinationCharges(clsGenFunction.get_checkStringNull(dgvAdditionalChargesDestination.CurrentRow.Cells["SalesTermSetupAdditionalChargesID"].Value));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvAdditionalChargesDestination_CellClick");
            }
        }

        private void frmODChargesCustomizer_FormClosed(object sender, FormClosedEventArgs e)
        {
            _BUS.Dispose();
            GC.Collect();
        }

        private void dgvAdditionalChargesOrigin_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            try
            {
                if (e.CurrentRow.Index > -1)
                {
                    set_CurrentRowchangedAdditionalCharges(e, ref dgvAdditionalChargesOrigin, "Origin");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvAdditionalChargesOrigin_CurrentRowChanged");
            }
        }

        private void dgvAdditionalChargesDestination_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            try
            {
                if (e.CurrentRow.Index > -1)
                {
                    set_CurrentRowchangedAdditionalCharges(e, ref dgvAdditionalChargesDestination, "Destination");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " dgvAdditionalChargesDestination_CurrentRowChanged");
            }
        }

        //private void dgvOrigin_RowsChanged(object sender, GridViewCollectionChangedEventArgs e)
        //{
        //    if (IsInitialized == true)
        //    {
        //        //if (dgvOrigin.CurrentRow.Cells["ccitemcode"].Value == null || dgvOrigin.CurrentRow.Cells["ccitemcode"].Value == DBNull.Value)
        //        //{
        //        //    for (int intCol = 6; intCol <= 17; intCol++)
        //        //    {
        //           //   dgvOrigin.CurrentCell.Value = 0;
        //           // }
        //       // }
        //    }
        //}
    }
}
